import Vue from "vue";
import $router from '../router/index.js'
let vm = new Vue();
import {sendPost,sendGet} from './request.js'
//注册用户
export let regestUser = (d)=> sendPost('/api/register',d)
//登录
export let denglu = (d) => sendPost('/api/login',d)
//获取轮播图 
export let getBanners = () => sendGet('/api/getbanner')
//获取商品信息
export let gethortgoods = () => sendGet('api/gethortgoods')

//首页所有商品一级分类：
export let getFirstCate = () => sendGet('/api/getcate');

//分类：获取商品的所有分类：
export let getAllCate = () => sendGet('/api/getcates');
//商品列表：根据某个二级商品类别id查询其下面的商品数据：
export let getSecondCateGoods = d => sendGet('/api/getgoodlist', d);
//获取一个商品信息
export let getGoodDetail = d => sendGet('/api/getgoodsinfo',d)
//添加到购物车
export let addCart = d => sendPost('/api/cartadd',d)
//获取购物车列表
export let cartList = d => sendGet('/api/cartlist',d)
//发送验证码
export let sms = d => sendGet('/api/sms',d) 
//购物车修改
export let cartEdit = d => sendPost('/api/cartedit',d)
//删除购物车
export let cartDelete = d => sendPost('/api/cartdelete',d)
//获取收货地址
export let addressList = d=> sendGet('/api/addresslist',d)
//添加收货地址
export let addRessadd = d => sendPost('/api/addressadd',d)
//编辑收货地址
export let addressEdit = d => sendPost('/api/addressedit',d)
//删除地址
export let addressRemove = d => sendPost('api/addressremove',d)
//查询
export let search = d => sendGet('/api/search',d)
//token
export let checktoken =d => sendGet('/api/checktoken',d)
//获取用户ID:
export let getCurUserId = (curpath = '/cart') => {
    //从localStorage中获取用户身份信息：
    let userinfoObj = localStorage['appUser'] ? JSON.parse(localStorage['appUser']) : '';
    if (userinfoObj == '') { //未登录
        let pathArr = ['/cart', '/my']; //需要验证的访问路径
        let indexval = pathArr.indexOf(curpath);
        if (indexval !== -1) { //
            vm.$toast.fail('请先登录...');
            // vm.$notify({ type: 'danger', message: '请先登录' });
                $router.push('/login'); //跳转到登录页面
            // $router.push('/login'); //跳转到登录页面
        } else {
            return '';
        }
    } else { //已登录
        return userinfoObj.uid; //用户id
    }
}